#!/usr/bin/env python
# encoding: utf-8

from common import Account
import sys
import os
import os.path
import random
import time

def Rand():
  return random.randint(0, sys.maxint)
  
def GenerateAlbumName():
  return "Album_%012x_%08x" % (int(time.time()), Rand())

class PhotoUploader(object):
  def __init__(self):
    self.album = None
    self.file_map = {}
    
  def GetIncompleteAlbum(self):
    if not self.album or self.album.Full():
      self.album = Account().NewAlbum(GenerateAlbumName())
    return self.album
    
  def UploadFile(self, filename):
    photo = self.GetIncompleteAlbum().UploadPhoto(filename)
    if photo:
      self.file_map[int(photo.gphoto_id.text)] = filename

  def UploadDirectory(self, path):
    for root, dirs, files in os.walk(path):
      map(lambda d: self.UploadDirectory(os.path.join(root, d)), dirs)
      map(lambda f: self.UploadFile(os.path.join(root, f)), files)

  def UploadPath(self, path):
    if os.path.isfile(path):
      self.UploadFile(path)
    else:
      self.UploadDirectory(path)

def main():
  uploader = Uploader()
  map(uploader.UploadPath, sys.argv[1:])

if __name__ == '__main__':
  main()
